package com.sig.util;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 随机序列号生成工具
 *
 * @author sig
 * @since 2019/1/26
 */
public final class CdKeyUtil {

    private CdKeyUtil() {
    }

    /**
     * 随机序列号生成器
     *
     * @param num       数量
     * @param prefixNum 前缀个数
     * @param patch     批次
     * @param suffix    尾数
     * @return
     */
    public static Set<Long> generate(final int num, final int prefixNum, final int patch, final int suffix) {
        final Set<Long> keyIds = new HashSet<>();
        int size = keyIds.size();
        while (size < num) {
            final StringBuilder builder = new StringBuilder();
            for (int index = 0; index < prefixNum; index++)
                builder.append(ThreadLocalRandom.current().nextInt(9) + 1);
            builder.append(patch).append(suffix);
            keyIds.add(Long.valueOf(builder.toString()));
            size++;
        }
        return keyIds;
    }
}
